#ifndef MY_CALLSTACK_H
#define MY_CALLSTACK_H

/*
 * !!!!! ONLY USE MACROS !!!!!
 * Use NEW_STACK() at begining
 * Only use PUSH() and POP()
 * Use DEL_STACK() in the end
 */

#define ENABLE_CALLSTACK // Open this function

#ifndef ENABLE_CALLSTACK

#define NEW_STACK(arg1) {}
#define DEL_STACK() {}
#define CS_PUSH() {}
#define CS_POP() {}
#define WHO_CALL_ME {}
#define BACKTRACE(arg1) {}
#define SHOW_ALL() {}

#else

#define NEW_STACK(arg1) do{new_callstack(arg1);}while(0)
#define DEL_STACK() do{delete_callstack();}while(0)
#define CS_PUSH() do{callstack_push((char*)__func__, __LINE__);}while(0)
#define CS_POP() do{callstack_pop(__LINE__);}while(0)
#define WHO_CALL_ME() do{show_last_item(1);}while(0)
#define BACKTRACE(arg1) do\
			{\
				fprintf(stderr,"-- backtrace --\n");\
				show_last_item(arg1);\
				fprintf(stderr,"===============\n");\
			}while(0)
#define SHOW_ALL() 	do\
			{\
				fprintf(stderr,"-- show all --\n");\
				show_all_item();\
				fprintf(stderr,"==============\n");\
			}while(0)
struct code_info
{
	char name[64];
	int line;
};
struct callstack
{
	unsigned int top;
	unsigned int size;
	struct code_info *item;
};

void callstack_push(char *func_name, int line_num);
void callstack_pop(int line_num);
void new_callstack(int size);
void delete_callstack();

void show_item_info(const int idx);
void show_last_item(const int num);
void show_all_item();
#endif

#endif
